﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class ImportTemplates : Form
    {
        public bool ImportNone
        {
            get
            {
                return rbtnNo.Checked;
            }
        }
        public string ImportFrom
        {
            get
            {                
                if (rbtnCurrentFile.Checked)
                {
                    return Program.CurrentFile.FileName;
                }
                if (rbtnAnotherFile.Checked)
                {
                    return _user_choice;
                }
                return String.Empty;
            }
        }
        private string _user_choice;
        
        public ImportTemplates()
        {
            InitializeComponent();
            rbtnCurrentFile.Text = rbtnCurrentFile.Text.Replace(
                "#",
                Path.GetFileName(Program.CurrentFile.FileName));
            ValidateInput();
            foreach (var _radiobutton in new RadioButton[] { rbtnNo, rbtnAnotherFile, rbtnCurrentFile })
            {
                _radiobutton.CheckedChanged += new EventHandler(_radiobutton_CheckedChanged);
            }
            tbxFileName.TextChanged += new EventHandler(tbxFileName_TextChanged);
        }

        void tbxFileName_TextChanged(object sender, EventArgs e)
        {
            ValidateInput();
        }

        void _radiobutton_CheckedChanged(object sender, EventArgs e)
        {
            ValidateInput();
        }
        
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (Program.ShowImportTemplatesDialog(System.IO.Path.GetFullPath(Program.CurrentFile.FileName)) == DialogResult.OK)
            {
                _user_choice = Program.GetSelectedFileName;
            }
            tbxFileName.Text = _user_choice;
        }

        private void ValidateInput()
        {
            var _anything_chosen = rbtnNo.Checked | rbtnAnotherFile.Checked | rbtnCurrentFile.Checked;
            var _existing_file_chosen = !rbtnAnotherFile.Checked |
                File.Exists(tbxFileName.Text);
            btnOK.Enabled = _anything_chosen & _existing_file_chosen;
        }
    }
}
